<template>
  <div class="pages companyMg">
    <div class="pageLevel" v-if="!sub">
      <!-- 头部 -->
      <!-- <div class="head">
        <breadcrumb></breadcrumb>
      </div> -->
      <!-- 操作框 -->
      <div class="operationBox tableSearch">
        <el-button type="primary" v-if="setRole('新增')" :disabled="dis" @click="routerPush('1', '')" size="mini">新增</el-button>
        <!-- <el-button type="primary" :disabled="dis" @click="deleteRows()" size="mini" >删除</el-button> -->
        <!-- <el-button type="primary" :disabled="dis" size="mini" @click="handleFenpei()" >分配到下级单位</el-button> -->
        <!-- <a href="javascript:;" class="file" >
          批量导入
          <input ref="uploading" @change="uploading($event)" type="file">
        </a>
        <el-button size="mini" type="primary" @click="donwloadMb()" >下载模板</el-button> -->

      </div>
      <!-- 搜索框 -->
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="" label-width="15px">
            <el-input class="input" v-model="selectdata.keyword" size="small" placeholder="请输入客户名称、编码"></el-input>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-select v-model="selectdata.shopTypes" multiple collapse-tags placeholder="请选择客户类型" size="small" style='width: 200px'>
              <el-option v-for="item in shopTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="入驻状态" label-width="100px">
            <el-select v-model="selectdata.entryStatus"   size="small" style='width: 200px'>
              <el-option v-for="item in entryStatusList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelect" :disabled="dis" class="Button">查询</el-button>
          </el-form-item>
          <!-- <el-form-item label="" label-width="15px">
            <el-button type="primary" size="mini" @click="showmore=!showmore">高级查询</el-button>
          </el-form-item> -->
        </el-form>
      </div>
      <!-- //高级搜索 -->
      <!-- <div class="tableSearch" v-show="showmore">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="企业客户分类:" label-width="165px" style="margin-left:40px">
            <el-select v-model="selectdata.typeId" placeholder="总代理" size="small">
              <el-option v-for="item in supplierType" :key="item.id" :value="item.id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div> -->
      <!-- 表格 -->
      <div class="tableMain">
        <el-table ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange" stripe style="width: 100%">
          <!-- <el-table-column type="selection" width="55" :selectable="puStatus"></el-table-column> -->
          <!-- <el-table-column type="index" label="序号" width="55"></el-table-column> -->
          <el-table-column prop="shopCode" label="企业客户编码" align="center"></el-table-column>
          <el-table-column prop="name" label="企业客户名称" align="center"></el-table-column>
          <el-table-column prop="status" align="center" label="是否启用">
            <template slot-scope="scope">
              <span v-if="scope.row.status == '0'">启用</span>
              <span v-if="scope.row.status == '1'">停用</span>
            </template>
          </el-table-column>
          <el-table-column prop="principal" align="center" label="联系人"></el-table-column>
          <el-table-column prop="linkPhone" align="center" label="联系电话"></el-table-column>
           <el-table-column prop="isLogin" align="center" label="入驻状态">
            <template slot-scope="scope">
              <span v-if="scope.row.isLogin == '0'">未入驻</span>
              <span v-if="scope.row.isLogin == '1'">已入驻</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="180">
            <template slot-scope="scope">
              <el-button size="small"  :disabled="dis" @click="routerPush('2',scope.row.id)" type="text" class="addDetail">查看</el-button>
              <!-- <el-button size="small" :disabled="dis" @click="routerPush('companyDetail',{status:'3',id:scope.row.id})" type="text" class="addDetail" >修改</el-button> -->
              <el-button size="small" v-if="setRole('销售价格协议')&&scope.row.status == '0'" :disabled="dis" @click="routerPushPrice('3',scope.row.id,scope.row.name)" type="text" class="addDetail">价格协议</el-button>
              <!-- <el-button size="small" :disabled="dis" @click="handleCheck(scope.row.id)" type="text" class="addDetail">审核</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码 -->
        <div class="pageblock">
          <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10,20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalItems">
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="pageLevel" v-else>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import util from "@/util";
import { mapState } from "vuex";
import state from "@/util/state";
import { selectBaseShopPageList, deleteBaseShop, selectShopTypeList } from "@/api";
export default {
  //   components: { pageUnit },
  computed: mapState(["sub", "power", "merchantId"]),
  data() {
    return {
      orgKey: "",
      orgId: "1",
      dialogVisible: false,
      dis: false,
      selectdata: {
        keyword: "",
        status: "",
        typeId: "",
        shopTypes: [],
        entryStatus:"",
      },
      searchData: "",
      supplierType: [],
      showmore: false,
      tableData: [],
      multipleSelection: [],
      multipleSelections: [],
      multipleSelection2: [],
      // 分页开始
      pageSize: 10,
      totalItems: 0,
      currentPage: 1,
      // 分页开始
      pageSizes: 20,
      totalItemss: 0,
      currentPages: 1,
      tableData1: [],
      gridData: [],
      shopTypeList:[],
      entryStatusList:[{
        name:"全部",
        id:""
      },
      {
        name:"未入驻",
        id:"0"
      },
      {
        name:"已入驻",
        id:"1"
      }]
    };
  },
  created() {
    this.getShopTypeList()
  },
  mounted() {
    this.tableget(this.pageSize, 1);
  },
  watch: {
    sub: {
      handler: function(val, oldVal) {
        if (oldVal != null && val == false) {
          this.currentPage = 1
          this.tableget(this.pageSize, 1);
        }
      },
      deep: true
    }
  },
  methods: {
    //配置权限
    setRole(key) {
      return util.selectTitle(this.power, key);
    },
    //路由跳转
    routerPush(status, id = "") {
      util.routerTo(
        "/operate-management/client-management/client-companyMg/client-companyDetail",
        {
          status: status,
          id: id
        }
      );
    },
    routerPushPrice(status, id = "",name) {
      util.routerTo(
        "/operate-management/client-management/client-companyMg/client-salePrice",
        {
          status: status,
          id: id,
          name:name,
        }
      );
    },
    puStatus(row) {
      return row.status == 0;
    },
    // 点击下载模板按钮
    donwloadMb() {
      let url = "";
      window.open(
        (url =
          "http://sd-picture-oss.oss-cn-beijing.aliyuncs.com/企业客户模板.xlsx")
      );
    },
    uploading(event) {
      this.file = event.target.files[0]; //获取文件
      var windowURL = window.URL || window.webkitURL;
      //创建图片文件的url
      this.src = windowURL.createObjectURL(event.target.files[0]);
      //取消默认行为
      // event.preventDefault();
      let formdata = new FormData();
      formdata.append("file", this.file);
      formdata.append("orgId", this.compId);
      this.loading = true;
      leadCustomer(formdata)
        .then(res => {
          this.$refs.uploading.value = null;
          this.loading = false;
          this.tableget(20, 1);
          this.$message({
            showClose: true,
            message: "导入成功！",
            type: "success"
          });
        })
        .catch(error => {})
        .finally((this.$refs.uploading.value = null));
    },
    changeFun(val) {
      this.isshop = true;
      this.multipleSelections = val;
    },
    handleFenpei() {
      if (this.multipleSelection.length > 0) {
        this.dialogVisible = true;
      } else {
        this.$message({
          showClose: true,
          message: "请先选择客户！",
          type: "error"
        });
      }
    },
    // 获取表格数据
    tableget(pageSize, pageNum) {
      let shopTypeIds = ''
      if(this.selectdata.shopTypes.length > 0){
        this.selectdata.shopTypes.forEach((val,index) => {
          if(index == this.selectdata.shopTypes.length-1) {
            shopTypeIds += `${val}`
          }else {
            shopTypeIds += `${val},`
          }
        });
      }

      let data = {
        nameOrCode: this.selectdata.keyword,
        pageNum: pageNum,
        pageSize: pageSize,
        openAccount: this.selectdata.openAccount,
        businessId: this.merchantId,
        type: 1, //1供应商（查找客户） 2-客户（查找供应商） 3-连锁（查客户）
        shopTypes: shopTypeIds,
        isLogin:this.selectdata.entryStatus
      };
      selectBaseShopPageList(data).then(res => {
        this.tableData = res.data.list;
        this.totalItems = res.data.pageTotal;
      });
    },
    toPage(val) {
      this.currentPage = val;
      this.tableget(this.pageSize, val);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.tableget(val, this.currentPage);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.tableget(this.pageSize, val);
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击删除按钮
    deleteRows() {
      // var arrId = []
      for (var i = 0; i < this.multipleSelection.length; i++) {
        this.multipleSelection[i].customerId = this.multipleSelection[i].shopId;
      }
      let idList = JSON.stringify(this.multipleSelection);
      let Data = new FormData();
      Data.append("id", idList);
      deleteBaseShop(Data).then(res => {
        this.$message({
          type: "success",
          message: res.msg
        });
        this.tableget(this.pageSize, 1);
      });
    },
    // 点击查询按钮
    searchSelect() {
      this.tableget(this.pageSize, this.currentPage);
    },
    getShopTypeList(){
      let that = this
      let data = {
        shopId: this.merchantId,
        name: '',
        status: ''
      }
      selectShopTypeList(data).then(res=>{
        if(res.code == 200){
          that.shopTypeList = res.data
        }
      })
    },
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
.file {
  position: relative;
  top: 10px;
  display: inline-block;
  background: #409eff;
  border: 1px solid #409eff;
  border-radius: 4px;
  padding: 4px 12px;
  margin: auto 10px;
  overflow: hidden;
  color: #fff;
  text-decoration: none;
  text-indent: 0;
  line-height: 18px;
  font-size: 10px;
}
.file input {
  position: absolute;
  right: 0;
  top: 0;
  opacity: 0;
}
.file:hover {
  background: #409eff;
  border-color: #409eff;
  color: #fff;
  text-decoration: none;
}
.operationBox {
  text-align: left;
}
</style>

